package com.zpark.controller.exception;

import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @PackageName: com.zpark.controller.exception
 * @ClassName: CustomExceptionHandlerController
 * @Description:
 * @author: RZS
 * @date: 2022/9/22  16:35
 */
//@Controller
@RestController
public class CustomExceptionHandlerController extends AbstractErrorController {
    // 定义错误映射路径
    private final static String ERROR_PATH = "/error";

    // 添加构造器
    public CustomExceptionHandlerController(ErrorAttributes errorAttributes) {
        // 调用父类构造器创建父类对象
        super(errorAttributes);
    }

    @Override
    public String getErrorPath() {
        return ERROR_PATH;
    }

    @RequestMapping(ERROR_PATH)
    public Map<String, Object> customHandler(HttpServletRequest request){
        Map<String, Object> map = getErrorAttributes(request, ErrorAttributeOptions.of(ErrorAttributeOptions.Include.MESSAGE));


//        // 定义返回的数据类型
//        Map<String, Object> map = new HashMap<>();
//        // 封装错误信息数据
//        map.put("code", 404);
//        map.put("mag", "");

        return map;
    }

}
